package net.goutalk.glcs.module.form.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.toolkit.MPJWrappers;
import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.form.dto.AddFormReleaseDto;
import net.goutalk.glcs.module.form.dto.FormReleasePageDto;
import net.goutalk.glcs.module.form.dto.UpdateFormReleaseDto;
import net.goutalk.glcs.module.form.entity.FormRelease;
import net.goutalk.glcs.module.form.entity.FormTemplate;
import net.goutalk.glcs.module.form.service.IFormReleaseService;
import net.goutalk.glcs.module.form.vo.FormReleasePageVo;
import net.goutalk.glcs.module.form.vo.FormReleaseVo;
import net.goutalk.glcs.module.system.entity.Menu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 自定义表单 发布表 前端控制器
 * </p>
 *
 * @author tanyujie
 * @since 2022-05-09
 */
@RestController
@RequestMapping(GlobalConstant.FORM_MODULE_PREFIX + "/release")
@Api(value = GlobalConstant.FORM_MODULE_PREFIX + "/release", tags = "自定义表单发布")
@AllArgsConstructor
public class FormReleaseController {

    private final IFormReleaseService formReleaseService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "表单发布列表(分页)")
    @GlcsLog(value = "获取表单发布列表")
    public R page(@Valid FormReleasePageDto dto) {
        //因为多表关联 会有多个表都使用了id字段，  所以必须专门指定主表的Id
        IPage<FormReleasePageVo> page = formReleaseService.selectJoinListPage(ConventPage.getPage(dto), FormReleasePageVo.class,
                MPJWrappers.<FormRelease>lambdaJoin()
                        .and(StrUtil.isNotBlank(dto.getKeyword()),
                                wrapper -> wrapper.like(Menu::getName, dto.getKeyword())
                                        .or().like(FormTemplate::getName, dto.getKeyword()))
                        .select(FormRelease::getId)
                        .select(FormRelease.class, x -> VoToColumnUtil.fieldsToColumns(FormReleasePageVo.class).contains(x.getProperty()))
                        .selectAs(Menu::getTitle,FormReleasePageVo::getMenuName)
                        .selectAs(FormTemplate::getName, FormReleasePageVo::getFormName)
                        .leftJoin(Menu.class, Menu::getId, FormRelease::getMenuId)
                        .leftJoin(FormTemplate.class, FormTemplate::getId, FormRelease::getFormId)
                        .orderByDesc(FormRelease::getCreateDate));

        PageOutput<FormReleasePageVo> pageOutput = ConventPage.getPageOutput(page);

        return R.ok(pageOutput);
    }


    @PostMapping
    @ApiOperation(value = "自定义表单发布")
    @GlcsLog(value = "自定义表单发布")
    public R add(@Valid @RequestBody AddFormReleaseDto dto) {
        return R.ok(formReleaseService.addFormRelease(dto));
    }

    @PutMapping
    @ApiOperation(value = "修改自定义表单发布")
    @GlcsLog(value = "自定义表单发布" )
    public R update(@Valid @RequestBody UpdateFormReleaseDto dto) {
        return R.ok(formReleaseService.updateFormRelease(dto));
    }


    @GetMapping(value = "/info")
    @ApiOperation(value = "表单发布详情")
    @GlcsLog(value = "表单发布详情")
    public R info(@RequestParam Long id) {
        //因为多表关联 会有多个表都使用了id字段，  所以必须专门指定主表的Id
        FormRelease release = formReleaseService.getById(id);
        if (release == null) {
            return R.error("表单发布数据不存在");
        }

        return R.ok(BeanUtil.toBean(release, FormReleaseVo.class));
    }


    @DeleteMapping
    @ApiOperation(value = "删除")
    @GlcsLog(value = "删除")
    public R delete(@Valid @RequestBody List<Long> ids) {
        return R.ok(formReleaseService.removeBatchByIds(ids));
    }

}
